package org.linlinjava.litemall.admin.service.order_manage;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.admin.vo.hospital_send.HospitalSendBackDto;
import org.linlinjava.litemall.admin.vo.vip.AddVipLevelVo;
import org.linlinjava.litemall.admin.vo.vip.ModifyUserVipLevelVo;
import org.linlinjava.litemall.admin.vo.vip.VipLevelListVo;
import org.linlinjava.litemall.admin.vo.vip.VipUserListVo;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.dao.AgedmallVipLevelMapper;
import org.linlinjava.litemall.db.dao.LitemallUserMapper;
import org.linlinjava.litemall.db.dao.order_manage.AgedmallVipMapper;
import org.linlinjava.litemall.db.domain.AgedmallVipLevel;
import org.linlinjava.litemall.db.domain.AgedmallVipLevelExample;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.dto.vip.VipLevelListBackDto;
import org.linlinjava.litemall.db.dto.vip.VipUserBackDto;
import org.linlinjava.litemall.db.dto.vip.VipUserListBackDto;
import org.linlinjava.litemall.db.vo.shop.HospitalSendDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wj
 * @date 2022/1/27 14:18
 */
@Service
@Transactional
public class AgedmallVipService {


    @Resource
    private AgedmallVipMapper agedmallVipMapper;

    @Resource
    private AgedmallVipLevelMapper agedmallVipLevelMapper;

    @Resource
    private LitemallUserMapper litemallUserMapper;



    public Object addVipLevel(AddVipLevelVo addVipLevelVo){
        AgedmallVipLevelExample levelExample=new AgedmallVipLevelExample();
        levelExample.createCriteria().andVipLevelNameEqualTo(addVipLevelVo.getVipName()).andDeletedEqualTo(false);
        AgedmallVipLevel vipLevel=agedmallVipLevelMapper.selectOneByExample(levelExample);
        if(ObjectUtil.isNotNull(vipLevel)){
            return ResponseUtil.fail(500,"当前名称已存在");
        }
        Integer level=1;
        AgedmallVipLevelExample getLastLevel=new AgedmallVipLevelExample();
        levelExample.createCriteria().andVipLevelNameEqualTo(addVipLevelVo.getVipName());
        getLastLevel.setOrderByClause("vip_level DESC");
        AgedmallVipLevel lastLevelBean=agedmallVipLevelMapper.selectOneByExample(getLastLevel);
        if(ObjectUtil.isNotNull(lastLevelBean)){
            level=lastLevelBean.getVipLevel()+1;
        }
        vipLevel=new AgedmallVipLevel();
        vipLevel.setVipLevelName(addVipLevelVo.getVipName());
        vipLevel.setVipLevel(level);
        vipLevel.setUseFlag(0);
        vipLevel.setAddTime(LocalDateTime.now());
        agedmallVipLevelMapper.insertSelective(vipLevel);
        return ResponseUtil.ok();
    }


    public Object modifyVipLevel(AddVipLevelVo addVipLevelVo){
        AgedmallVipLevel vipLevel = agedmallVipLevelMapper.selectByPrimaryKey(addVipLevelVo.getId());
        AgedmallVipLevelExample levelExample=new AgedmallVipLevelExample();
        levelExample.createCriteria().andVipLevelNameEqualTo(addVipLevelVo.getVipName()).andDeletedEqualTo(false);
        AgedmallVipLevel nameIsExist=agedmallVipLevelMapper.selectOneByExample(levelExample);
        if(ObjectUtil.isNotNull(nameIsExist)&&!addVipLevelVo.getVipName().equals(vipLevel.getVipLevelName())){
            return ResponseUtil.fail(500,"当前名称已存在");
        }
        vipLevel.setVipLevelName(addVipLevelVo.getVipName());
        vipLevel.setUseFlag(addVipLevelVo.getUseFlag());
        vipLevel.setAddTime(LocalDateTime.now());
        agedmallVipLevelMapper.updateByPrimaryKeySelective(vipLevel);
        return ResponseUtil.ok();
    }

    public Object vipLevelList(VipLevelListVo vipLevelListVo){
        Map<String, Object> map = new HashMap<>();
        Page page = PageHelper.startPage(vipLevelListVo.getPage(), vipLevelListVo.getLimit());
        List<VipLevelListBackDto> vipLevelListBackDtos = agedmallVipMapper.selectVipLevelList(vipLevelListVo.getKeySearch(),vipLevelListVo.getUseFlag());
        map.put("total", page.getTotal());
        map.put("data", vipLevelListBackDtos);
        return ResponseUtil.ok(map);
    }

    public Object deleteVipUser(Integer id){
        LitemallUser user=litemallUserMapper.selectByPrimaryKey(id);
        user.setUserLevel((byte)0);
        litemallUserMapper.updateByPrimaryKeySelective(user);
        return ResponseUtil.ok();
    }


    public Object searchVipUserByPhone(String phone){
        VipUserBackDto userLevelByPhone = agedmallVipMapper.getUserLevelByPhone(phone);
        return ResponseUtil.ok(userLevelByPhone);
    }

    public Object modifyUserVipLevel(ModifyUserVipLevelVo modifyUserVipLevelVo){
        LitemallUser user=litemallUserMapper.selectByPrimaryKey(modifyUserVipLevelVo.getId());
        user.setUserLevel(modifyUserVipLevelVo.getVipLevelId().byteValue());
        litemallUserMapper.updateByPrimaryKeySelective(user);
        return ResponseUtil.ok();
    }


    public Object vipUserList( VipUserListVo vipUserListVo){
        Map<String, Object> map = new HashMap<>();
        Page page = PageHelper.startPage(vipUserListVo.getPage(), vipUserListVo.getLimit());
        List<VipUserListBackDto> vipUserListBackDtos = agedmallVipMapper.selectVipUserList(vipUserListVo.getKeySearch());
        map.put("total", page.getTotal());
        map.put("data", vipUserListBackDtos);
        return ResponseUtil.ok(map);
    }

}
